﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Payton.WP7.Common.Interface;
using Hammock;
using Payton.WP7.Common.Model;
using Newtonsoft.Json.Linq;
using System.Collections.Generic;
using System.Linq;
using Hammock.Authentication.OAuth;

namespace Payton.WP7.Common.Tencent
{
    public class TencentUserService : UserServiceBase
    {
        protected override RestClient CreateRestClient()
        {
            return new RestClient
            {
                Authority = "http://open.t.qq.com",   // Tencent调用API的时候不是用的https协议，这点和获取Token方式不同
                HasElevatedPermissions = true
            };
        }

        protected override void Initialize()
        {
            GET_CURRENT_USER = "/api/user/info?format=json";
            GET_USER = "/api/user/other_info?format=json";
            FOLLOW_USER = "/api/friends/add?format=json";
            UNFOLLOW_USER = "/api/friends/del?format=json";
            FRIENDSHIP = "";
            GET_USER_LIST = "/api/user/infos?format=json";
            GET_FANS = "/api/friends/fanslist?format=json";
            GET_FANS_LITE = "/api/friends/fanslist_s?format=json";
            GET_IDOLS = "/api/friends/idollist?format=json";
            GET_IDOLS_LITE = "/api/friends/idollist_s?format=json";
            GET_USER_FANS = "/api/friends/user_fanslist?format=json";
            GET_USER_IDOLS = "/api/friends/user_idollist?format=json";
            GET_USER_SPECIALS = "/api/friends/user_speciallist_s?format=json";
        }

        protected override string[] GetParamtersList(Model.SNSType type)
        {
            string[] list;
            switch (type)
            {
                case SNSType.SingleUser:
                    list = new string[] { 
                        SNSParametersConstants.NAME
                    };
                    break;
                case SNSType.Friends:
                    list = new string[] { 
                        SNSParametersConstants.FORMAT,
                        SNSParametersConstants.REQ_NUM,
                        SNSParametersConstants.START_INDEX,
                        SNSParametersConstants.NAME,
                    };
                    break;
                case SNSType.Followers:
                    list = new string[] { 
                        SNSParametersConstants.FORMAT,
                        SNSParametersConstants.REQ_NUM,
                        SNSParametersConstants.START_INDEX,
                        SNSParametersConstants.NAME,
                    };
                    break;
                case SNSType.UserFriends:
                    list = new string[] { 
                        SNSParametersConstants.FORMAT,
                        SNSParametersConstants.REQ_NUM,
                        SNSParametersConstants.START_INDEX,
                        SNSParametersConstants.NAME,
                    };
                    break;
                case SNSType.UserFollowers:
                    list = new string[] { 
                        SNSParametersConstants.FORMAT,
                        SNSParametersConstants.REQ_NUM,
                        SNSParametersConstants.START_INDEX,
                        SNSParametersConstants.NAME,
                    };
                    break;
                case SNSType.HotPeople:
                    return new string[]
                    { 
                        SNSParametersConstants.CONTENT,
                        SNSParametersConstants.CLIENT_IP,
                        SNSParametersConstants.JING,
                        SNSParametersConstants.WEI,
                        SNSParametersConstants.REID
                    };
                case SNSType.Follow:
                    return new string[]
                    { 
                        SNSParametersConstants.CLIENT_IP,
                        SNSParametersConstants.NAME
                    };

                case SNSType.UnFollow:
                    return new string[]
                    { 
                        SNSParametersConstants.CLIENT_IP,
                        SNSParametersConstants.NAME
                    };
                case SNSType.SuggestionPeoples:
                    list = new string[] { 
                        //SNSParametersConstants.COUNT,
                        //SNSParametersConstants.BASE_APP
                    };
                    break;
                default:
                    list = new string[] { };
                    break;
            }
            return list;
        }

        protected override System.Collections.Generic.IList<Model.SNSUser> Mapping(string res, SNSResult result)
        {
            JObject o = JObject.Parse(res);

            List<Model.SNSUser> users = o.SelectToken("data.info").Select(
                s => factory.GetMapping<JToken, Model.SNSUser>().Map(s)
                ).ToList();
            return users;
        }

        protected override SNSUser MappingSingle(string res,SNSResult result)
        {
            JObject o = JObject.Parse(res);

            Model.SNSUser user = factory.GetMapping<JToken, Model.SNSUser>().Map(o.SelectToken("data"));
                
            return user;
        }

        protected override Hammock.Authentication.IWebCredentials CreateCredential()
        {
            var credantial = base.CreateCredential() as OAuthCredentials;
            credantial.ParameterHandling = OAuthParameterHandling.UrlOrPostParameters;
            return credantial;
        }

    }
}
